package ServerStuff;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import DataStorageClasses.Client;
import Database.DatabaseConnector;


@WebServlet("/AddClient")
public class AddClient extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		DatabaseConnector dbConnect = new DatabaseConnector();
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession();
		Boolean loggedIn = (Boolean) session.getAttribute("loggedIn");
		
		String busName = (String) session.getAttribute("username");
		
		String first = request.getParameter("first");
		String last = request.getParameter("last");
		String email = request.getParameter("email");
		String street = request.getParameter("street");
		String city = request.getParameter("city");
		String prov = request.getParameter("prov");
		String post = request.getParameter("post");
		String tel1 = request.getParameter("tel1");
		String tel2 = request.getParameter("tel2");
		String cell = request.getParameter("cell");
		
		Client c = null;
		if(busName != null)
		{
			c = dbConnect.addClient(first, last, email, street, city, prov, 
											post, tel1, tel2, cell, busName);
		}
		
		if( c != null)
    	{
    		//great!
    		res.sendRedirect("/GoGoGadget/ClientList.jsp");
    	}
    	else
    	{
    		if((loggedIn == null) || (loggedIn == false)){
    			res.sendRedirect("/GoGoGadget/StartPage.jsp");
    		}
    	}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
